package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.MyIncomeData;
import com.alm.token.ui.data.bean.QuotaPageData;
import com.alm.token.ui.data.bean.QuotaWalletIndexData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.ExchangeQuotaView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import baibeiwallet.event.getUserWalletEvent;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExchangeQuotaActivity extends BaseActivity implements ExchangeQuotaView<QuotaWalletIndexData> {

    @BindView(R.id.leftItem)
    ImageView leftItem;
    @BindView(R.id.centerItem)
    TextView centerItem;
    @BindView(R.id.rightItem)
    TextView rightItem;
    @BindView(R.id.ly_topshow)
    LinearLayout lyTopshow;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;

    @BindView(R.id.tv_canusedquota1)
    TextView tvCanusedquota1;
    @BindView(R.id.ly_bannertop1)
    RelativeLayout bannertop1Body;

    private View headView;
    private TextView tvCanusedquota;
    private RefreshLoadMoreManager<QuotaPageData.DataBean.ListBean> manager;
    private List<QuotaPageData.DataBean.ListBean> dataBeans = new ArrayList<>();

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_exchangequotalist;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.mine_hint_myed));
        headView = LayoutInflater.from(this).inflate(R.layout.activity_exchangequota_headview, (ViewGroup) recyclerView.getParent(), false);
        tvCanusedquota = headView.findViewById(R.id.tv_canusedquota);
        swipeRefreshLayout.setVisibility(View.GONE);
        initManager();
    }

    private void initManager() {
        GroupAdapter adapter = new GroupAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .build();
//        recyclerView.setNestedScrollingEnabled(false);
        adapter.addHeaderView(headView);
    }

    @Override
    protected void loadData() {
        preenter.getExchangeQuotaWalletIndexData();
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void getData(QuotaWalletIndexData quotaWalletIndexData) {

        if (quotaWalletIndexData.getStatus() == 0 && EmptyUtils.isNotEmpty(quotaWalletIndexData.getData().getMoney())) {
            tvCanusedquota.setText("$"+CommonUtil.saveToPoint4WithNo(quotaWalletIndexData.getData().getMoney()));
            tvCanusedquota1.setText("$"+CommonUtil.saveToPoint4WithNo(quotaWalletIndexData.getData().getMoney()));
        }
        preenter.getQuotaPageData();
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @Override
    public void getQuotaPage(QuotaPageData quotaPageData) {

        if (quotaPageData.getStatus() == 0 && quotaPageData.getData() != null) {

            if (EmptyUtils.isNotEmpty(quotaPageData.getData().getList())) {
                dataBeans = quotaPageData.getData().getList();
            }


            if (dataBeans != null) {
                for (int i = 0; i < dataBeans.size(); i++) {
                    dataBeans.get(i).setLayoutId(GroupAdapter.LAYOUT_ID);
                }
                manager.setData(dataBeans);
            }
            if (dataBeans.size() == 0) {
                bannertop1Body.setVisibility(View.VISIBLE);
                swipeRefreshLayout.setVisibility(View.GONE);
                nodatashowBody.setVisibility(View.VISIBLE);
            } else {
                bannertop1Body.setVisibility(View.GONE);
                swipeRefreshLayout.setVisibility(View.VISIBLE);
                nodatashowBody.setVisibility(View.GONE);
            }
        } else {
            showToast(quotaPageData.getMsg());
        }

    }


    @OnClick({R.id.tv_clickreload,R.id.tv_outmoneycommit})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tv_clickreload:
                loadData();
                break;
            case R.id.tv_outmoneycommit:
                //币币兑换 闪兑
                Intent intent=new Intent(this,TransformActivity.class);
                intent.putExtra("intentType",TransformActivity.EXCHANGEQUOTA_TYPE);
                startActivity(intent);
                break;
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    //     EventBus.getDefault().post(new getUserWalletEvent(null));
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserWalletEvent1(getUserWalletEvent userWalletEvent) {
        loadData();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public class GroupAdapter extends BaseAdapter<QuotaPageData.DataBean.ListBean> {

        public static final int LAYOUT_ID = R.layout.item_income_quota;

        public GroupAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, QuotaPageData.DataBean.ListBean item, int itemType) {

            helper.setText(R.id.tv_channelType, item.getChannelType());
            helper.setText(R.id.tv_createTime, CommonUtil.getDateTimeFromMillisecond(item.getCreateTime()));
            double money = item.getMoney();
            if (money < 0) {
                helper.setTextColor(R.id.tv_money, getResources().getColor(R.color.lehuobao_zhichu));
                helper.setImageDrawable(R.id.icon, getResources().getDrawable(R.drawable.icom_outdown));
                helper.setText(R.id.tv_money, CommonUtil.saveToPoint4WithNo(money));
            } else {
                helper.setTextColor(R.id.tv_money, getResources().getColor(R.color.colorgreen1));

                helper.setImageDrawable(R.id.icon, getResources().getDrawable(R.drawable.icom_getup));
                helper.setText(R.id.tv_money, "+" + CommonUtil.saveToPoint4WithNo(money));
            }
        }

    }
}
